package com.sword.test.testcase.demo;

import com.sword.commons.okhttp.HttpResponse;
import com.sword.test.testcase.TestCase;
import com.sword.woodpecker.autotest.framework.object.apiobject.BaseHeader;
import com.sword.woodpecker.autotest.framework.object.apiobject.HttpGet;
import com.sword.woodpecker.autotest.framework.object.extend.HttpExtend;
import com.sword.woodpecker.autotest.framework.object.extend.HttpGetExtend;
import java.net.MalformedURLException;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.annotations.Test;


/**
 * 类描述:〈使用内置的请求类来发送Http请求，并且将消息头保存到公共Header中〉
 *
 * @author: xujinjian
 */
public class Demo5TC extends TestCase {

    /**
     * 发送Get请求类
     */
    @Autowired
    protected HttpGet httpGet;

    /**
     * 发送Get请求类，会自动带上BaseHeader类中的公共消息头
     */
    @Autowired
    protected HttpGetExtend httpGetExtend;

    /**
     * Http相关功能扩展类
     */
    @Autowired
    protected HttpExtend httpExtend;

    private String host = "http://www.baidu.com";


    @Test
    public void test1() {
        //先发送get请求
        String url = host + "/s";
        String param = "ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=testtest&fenlei=256&oq=%25E8%2587%25AA%25E5%258A%25A8%25E5%258C%2596%25E6%25B5%258B%25E8%25AF%2595&rsv_pq=8431637f00014d34&rsv_t=ea57G2yAdcAtfdM9gw5UiplinP1m07tGwsNsiIexzUJHAtHC7vQv0agj1eg&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_btype=t&inputT=8137&rsv_sug3=35&rsv_sug1=39&rsv_sug7=101&rsv_sug4=8830";
        //httpGet类中，含有多种get请求方式，具体的看方法参数，选择适合自己的
        HttpResponse httpResponse = httpGet.get(url, param);
        logger.info("请求响应状态码为[{}]", httpResponse.getCode());
        if (httpResponse.getCode() == 200) {
            logger.info("请求响应Body为[{}]", httpResponse.getBody());
        }
        //再将消息头放入BaseHeader中
        try {
            //获取出请求的域名
            String domain = httpExtend.getHost(url);
            String headerKey = "消息头的name";
            String headerValue = "消息头的value";
            BaseHeader.add(domain, headerKey, headerValue);

            //使用httpGetExtend类发送get请求，会自动带上BaseHeader类中的公共消息头
            httpResponse = httpGetExtend.get(url);
            logger.info("请求响应状态码为[{}]", httpResponse.getCode());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }


}
